import numpy as np
def box_cut(box,cloud_in,scale=1.0):
    """
    input:
        box: array, shape=(7,)  (x, y, z, l, w, h, yaw)
        cloud: array, shape(N,M), (x, y, z, intensity, ...)
        scale: float, factor to enlarge the box size
    output:
        pts_in: array, points in box
        pts_out: array, points outside box
    """

    cloud=np.zeros(shape=(cloud_in.shape[0],4))
    cloud[:,0:3]=cloud_in[:,0:3]
    cloud[:,3]=1

    x, y, z, l, w, h, yaw = box[0],box[1],box[2],box[3],box[4],box[5],box[6]

    trans_mat=np.eye(4,dtype=np.float32)
    trans_mat[0, 0] = np.cos(yaw)
    trans_mat[0, 1] = -np.sin(yaw)
    trans_mat[0, 3] = x
    trans_mat[1, 0] = np.sin(yaw)
    trans_mat[1, 1] = np.cos(yaw)
    trans_mat[1, 3] = y
    trans_mat[2, 3] = z

    trans_mat_i=np.linalg.inv(trans_mat)
    cloud=np.matmul(cloud,trans_mat_i.T)

    mask_l = np.logical_and(cloud[:, 0] < l * scale / 2, cloud[:, 0] > -l * scale / 2)
    mask_w = np.logical_and(cloud[:,1]< w*scale/2,cloud[:,1]>-w*scale/2)
    mask_h = np.logical_and(cloud[:, 2] < h*scale/2, cloud[:, 2] > -h*scale/2)
    mask = np.logical_and(np.logical_and(mask_w,mask_l),mask_h)
    mask_not = np.logical_not(mask)
    pts_in = cloud_in[mask]
    pts_out = cloud_in[mask_not]

    return pts_in,pts_out